#if !defined(_H_CG2D)
#define _H_CG2D
#include "CType.h";
#include "CByteBuffer.h";
//represent a area. when 
class CG2DArea{
private:
	CG2DArea(){};
	int VertNum;
	float* Vert;
public:
	int Type;	
	static CG2DArea* getInstance(int type, const float* vert, int len){
		if(len > 0){
			CG2DArea* area = new CG2DArea();
			area->Type = type;
			area->Vert = new float[len];
			if(type == CG2DAREA_TYPE_POLYGON){
				area->VertNum = len/2;
				for(int i = 0; i< area->VertNum; i++){
					area->Vert[i] = vert[i*2];
					area->Vert[area->VertNum + i] = vert[i*2+1];
				}
			}else{
				area->VertNum = 0;
				MEMCOPY(vert,area->Vert,len);
			}
			return area;
		}
		return NULL;
	};
	~CG2DArea(){
		if(Vert != NULL){
			delete[] Vert;
			Vert = NULL;
		}
	};
	bool checkIn(float x, float y){
		if(Type == CG2DAREA_TYPE_RECT){
			return CHECK_POINT_IN_RECT(x,y,Vert[0],Vert[1],Vert[2],Vert[3]);
		}else if(Type == CG2DAREA_TYPE_CIRCLE){
			return CHECK_POINT_IN_CIRCLE(x,y,Vert[0],Vert[1],Vert[2]);
		}else if(Type == CG2DAREA_TYPE_POLYGON){
			return pnpoly(VertNum/2, Vert, Vert+VertNum/2, x, y);
		}
		return false;
	};
};
class StreamFilter{
public:
	int _size;
	int* _indexs;
	StreamFilter(int size){
		if(size > 0){
			_size = size;
			_indexs = new int[size]; 
			for(int i = 0; i< size; i++){
				_indexs[i] = -1;
			}
		}else{
			_size = 0;
			_indexs = NULL;
		}
	};
	~StreamFilter(){
		_size = 0;
		if(_indexs != NULL){
			delete[] _indexs;
			_indexs = NULL;
		}
	};
};
///
class PolygonStream{
public:
	static int checkIn(int nof_polygon, const char* stream, float x, float y,StreamFilter* index){
		if(index != NULL && index->_size >= nof_polygon){
			int pos = 0;
			int rsid = -1;
			for(int i = 0; i< nof_polygon; i++){
				int pid = DECODE_INT16(*(stream + pos),*(stream + pos + 1));
				pos += 2;

				int nof_vert = DECODE_INT16(*(stream + pos),*(stream + pos + 1));
				pos += 2;

				float* vert = (float*)(stream + pos);
				if(pnpoly(nof_vert, vert, (vert + nof_vert), x, y)){
					if(rsid == -1)rsid = pid;
					index->_indexs[i] = pid;
				}
				pos += (nof_vert*sizeof(float)*2);
			}
			//delete buf;
			return rsid;
		}
		return -2;
	};
	static int checkIn(int nof_polygon,const char* stream, float x, float y){
		int pos = 0;
		//CByteBuffer* buf = CByteBuffer::swap(stream,stream_len);
		int rsid = -1;
		for(int i = 0; i< nof_polygon; i++){
			//int pid = RINT16(buf);
			//int nof_vert = RINT16(buf);
			int pid = DECODE_INT16(*(stream + pos),*(stream + pos + 1));
			pos += 2;

			int nof_vert = DECODE_INT16(*(stream + pos),*(stream + pos + 1));
			pos += 2;

			//printf("polygon %d, num of vert %d\n", i, nof_vert);
			float* vert = (float*)(stream + pos);
			if(pnpoly(nof_vert, vert, (vert + nof_vert), x, y)){
				rsid = pid;
				break;
			}
			pos += (nof_vert*sizeof(float)*2);
		}
		//delete buf;
		return rsid;
	};
	static int checkInSkipHeaderBytes(int header_len, int nof_polygon, const char* stream, float x, float y,StreamFilter* index){
		if(index != NULL && index->_size >= nof_polygon){
			int pos = 0;
			int rsid = -1;
			for(int i = 0; i< nof_polygon; i++){
				pos += header_len;
				int pid = DECODE_INT16(*(stream + pos),*(stream + pos + 1));
				pos += 2;

				int nof_vert = DECODE_INT16(*(stream + pos),*(stream + pos + 1));
				pos += 2;

				float* vert = (float*)(stream + pos);
				if(pnpoly(nof_vert, vert, (vert + nof_vert), x, y)){
					if(rsid == -1)rsid = pid;
					index->_indexs[i] = pid;
				}
				pos += (nof_vert*sizeof(float)*2);
			}
			//delete buf;
			return rsid;
		}
		return -2;
	};
	static int checkInSkipHeaderBytes(int header_len, int nof_polygon,const char* stream, float x, float y){
		//CByteBuffer* buf = CByteBuffer::swap(stream,stream_len);
		int pos = 0;
		int rsid = -1;
		for(int i = 0; i< nof_polygon; i++){
			//buf->pass(header_len);
			pos += header_len;
			int pid = DECODE_INT16(*(stream + pos),*(stream + pos + 1));
			pos += 2;

			int nof_vert = DECODE_INT16(*(stream + pos),*(stream + pos + 1));
			pos += 2;
			//printf("polygon %d, num of vert %d\n", i, nof_vert);
			float* vert = (float*)(stream + pos);
			if(pnpoly(nof_vert, vert, (vert + nof_vert), x, y)){
				rsid = pid;
				break;
			}
			pos += (nof_vert*sizeof(float)*2);
		}
		//delete buf;
		return rsid;
	};
};
#endif //_H_CG2D